package com.wt.admin.controller.language;


import cn.hutool.core.util.StrUtil;
import com.aizuda.easy.security.domain.Rep;
import com.aizuda.easy.security.util.LocalUtil;
import com.wt.admin.code.language.QA2200;
import com.wt.admin.config.aspect.annotation.LogAno;
import com.wt.admin.domain.dto.language.QADTO;
import com.wt.admin.domain.dto.language.QATestTrainingDTO;
import com.wt.admin.domain.dto.language.QATrainingDTO;
import com.wt.admin.domain.dto.language.SentenceConfigDTO;
import com.wt.admin.domain.vo.language.ClassificationVO;
import com.wt.admin.domain.vo.language.QAListVO;
import com.wt.admin.domain.vo.language.QAParseSentenceVO;
import com.wt.admin.domain.vo.language.QAVO;
import com.wt.admin.service.ai.impl.agents.easyai.api.EasyAIApi;
import com.wt.admin.service.language.LanguageProxyService;
import com.wt.admin.util.AssertUtil;
import com.wt.admin.util.PageUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Instant;
import java.util.List;

import static com.wt.admin.service.language.impl.LanguageTrainingService.QA;

@RestController
@RequestMapping("qa")
public class QAController {

    @Resource
    private LanguageProxyService languageProxyService;

    @LogAno(name = "添加问答")
    @PostMapping("add")
    public Rep<QAVO> add(@RequestBody QADTO data){
        return Rep.ok(languageProxyService.getQAService().add(data));
    }

    @LogAno(name = "问答列表")
    @PostMapping("list")
    public Rep<QAListVO> list(@RequestBody PageUtil.PageDTO<QADTO> data){
        return Rep.ok(languageProxyService.qaList(data));
    }

    @LogAno(name = "删除问答")
    @PostMapping("del")
    public Rep<QAVO> del(@RequestBody QADTO data){
        return Rep.ok(languageProxyService.getQAService().del(data));
    }

    @LogAno(name = "编辑问答")
    @PostMapping("edit")
    public Rep<QAVO> edit(@RequestBody QADTO data){
        return Rep.ok(languageProxyService.getQAService().edit(data));
    }

    @LogAno(name = "训练样本")
    @PostMapping("qaTraining")
    public Rep<ClassificationVO> qaTraining(@RequestBody QATrainingDTO data){
        AssertUtil.List.isEmpty(data.getId(), QA2200.CODE_2205);
        languageProxyService.qaTraining(data, LocalUtil.getUser());
        return Rep.ok();
    }

    @LogAno(name = "测试样本")
    @PostMapping("qaTestTraining")
    public Rep<QAParseSentenceVO> qaTestTraining(@RequestBody QATestTrainingDTO data){
        return Rep.ok(languageProxyService.qaTestTraining(data));
    }

    @LogAno(name = "查询配置")
    @PostMapping("findConfig")
    public Rep<SentenceConfigDTO> findConfig(@RequestBody SentenceConfigDTO data){
        return Rep.ok(languageProxyService.findConfig(QA));
    }


    @PostMapping("/api/chat")
    EasyAIApi.ChatResponse chat(@RequestBody EasyAIApi.ChatRequest request) {
        long start = System.currentTimeMillis();
        String meg = request.messages().get(request.messages().size()-1).content();
        QATestTrainingDTO qaTestTrainingDTO = new QATestTrainingDTO();
        qaTestTrainingDTO.setData(meg);
        qaTestTrainingDTO.setModel(request.model());
        String answer = languageProxyService.qaTestTraining(qaTestTrainingDTO).getAnswer();
        if(StrUtil.isBlank(answer)){
            answer = "没有相关内容。";
        }
        return new EasyAIApi.ChatResponse(request.model(),
                Instant.now(),  new EasyAIApi.Message(EasyAIApi.Message.Role.ASSISTANT,answer, List.of(),List.of()), "stop", true,  System.currentTimeMillis() - start, System.currentTimeMillis() - start,
                0,  0L,  0, 0L);
    }

    @PostMapping(value = "/api/streamChat",produces = "application/json;charset=UTF-8")
    Flux<EasyAIApi.ChatResponse> streamChat(@RequestBody EasyAIApi.ChatRequest request) {
        long start = System.currentTimeMillis();
        String meg = request.messages().get(request.messages().size()-1).content();
        QATestTrainingDTO qaTestTrainingDTO = new QATestTrainingDTO();
        qaTestTrainingDTO.setData(meg);
        qaTestTrainingDTO.setModel(request.model());
        String answer = languageProxyService.qaTestTraining(qaTestTrainingDTO).getAnswer();
        if(StrUtil.isBlank(answer)){
            answer = "没有相关内容。";
        }
        return Flux.just(new EasyAIApi.ChatResponse(request.model(),
                Instant.now(),  new EasyAIApi.Message(EasyAIApi.Message.Role.ASSISTANT,answer,List.of(),List.of()), "stop", true,  System.currentTimeMillis() - start, System.currentTimeMillis() - start,
                0,  0L,  0, 0L));
    }

}
